package com.zyk.scaffold.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zyk.scaffold.common.enums.TenantTypeEnum;
import com.zyk.scaffold.user.domain.FunctionMessage;
import com.zyk.scaffold.user.entity.Function;
import com.zyk.scaffold.user.entity.Tenant;
import com.zyk.scaffold.user.entity.TenantFunction;
import com.zyk.scaffold.user.mapper.TenantFunctionMapper;
import com.zyk.scaffold.user.service.FunctionService;
import com.zyk.scaffold.user.service.TenantFunctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyk.scaffold.user.service.TenantService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 租户-功能表 服务实现类
 * </p>
 *
 * @author zhouyk
 * @since 2022-11-04
 */
@Service
public class TenantFunctionServiceImpl extends ServiceImpl<TenantFunctionMapper, TenantFunction> implements TenantFunctionService {

    @Autowired
    public FunctionService functionService;

    @Autowired
    public TenantService tenantService;

    @Override
    public List<Function> getFunctionsByTenantCode(String tenantCode) {
        List<Long> functionIds = this.list(Wrappers
                .lambdaQuery(TenantFunction.class)
                .eq(TenantFunction::getDeleted, Boolean.FALSE)
                .eq(TenantFunction::getTenantCode, tenantCode)
        ).stream().map(TenantFunction::getFunctionId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(functionIds)){
            return Collections.emptyList();
        }
        return functionService.listByIds(functionIds);
    }

    @Override
    public List<FunctionMessage> queryTenantCodesFunction(List<String> tenantCodes) {
        if(CollectionUtils.isEmpty(tenantCodes)){
            return Collections.emptyList();
        }
        List<FunctionMessage> functionMessages = this.baseMapper.queryTenantCodesFunction(tenantCodes);
        return functionMessages;
    }

    @Override
    public List<TenantFunction> getTenantFunctionsByTenantCode(String tenantCode) {
        return this.list(Wrappers
                .lambdaQuery(TenantFunction.class)
                .eq(TenantFunction::getDeleted, Boolean.FALSE)
                .eq(TenantFunction::getTenantCode, tenantCode)
        ).stream().collect(Collectors.toList());
    }

    @Override
    public Boolean syncSuperTenantFunctionData(List<Long> addIds, List<Long> deletedIds) {
        addIds = addIds.stream().distinct().collect(Collectors.toList());
        deletedIds = deletedIds.stream().distinct().collect(Collectors.toList());
        List<Tenant> tenants = tenantService.getTenantInfosByType(TenantTypeEnum.INNER_SUPER.getCode());
        if(CollectionUtils.isEmpty(tenants)) {
            return Boolean.TRUE;
        }
        List<String> tenantCodes = tenants.stream().map(Tenant::getCode).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(addIds)){
            List<TenantFunction> tenantFunctions = new ArrayList<>();
            for (Long addId : addIds) {
                for (String tenantCode : tenantCodes) {
                    TenantFunction tenantFunction = new TenantFunction();
                    tenantFunction.setFunctionId(addId);
                    tenantFunction.setTenantCode(tenantCode);
                    tenantFunction.setCreateUser(0L);
                    tenantFunctions.add(tenantFunction);
                }
            }
            this.saveBatch(tenantFunctions);
        }
        if(CollectionUtils.isNotEmpty(deletedIds)){
            this.lambdaUpdate()
                    .in(TenantFunction::getTenantCode, tenantCodes)
                    .in(TenantFunction::getFunctionId, deletedIds)
                    .set(TenantFunction::getDeleted, Boolean.TRUE)
                    .update();
        }
        return Boolean.TRUE;
    }
}
